#include "boids.h"
#include <iostream>
#include "globals.h"


EventReciever::EventReciever(){}


bool EventReciever::OnEvent(const SEvent& event)
{
	/*
	The sbRange_cohes ID is: 1
	The sbScale_cohes ID is: 2
	The sbRange_Sep ID is: 3
	The sbScale_Sep ID is: 4
	The sbRange_Align ID is: 5
	The sbScale_Align ID is: 6
	*/
	if (event.EventType == EET_GUI_EVENT)
	{
		s32 id = event.GUIEvent.Caller->getID();
		IGUIEnvironment* env = device->getGUIEnvironment();
		if (event.GUIEvent.EventType == EGET_SCROLL_BAR_CHANGED)
		{
			if (id == 1)
			{
				s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
				range_cohes = (float)pos;
				((IGUIScrollBar*)event.GUIEvent.Caller)->setToolTipText(stringw(range_cohes).c_str());
				return false;
			}

			else if (id == 2)
			{
				s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
				scaleFac_cohes = (float)pos/1000.0f;
				((IGUIScrollBar*)event.GUIEvent.Caller)->setToolTipText(stringw(scaleFac_cohes).c_str());
				return false;

			}
			else if (id == 3)
			{
				s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
				range_sep = (float)pos;
				((IGUIScrollBar*)event.GUIEvent.Caller)->setToolTipText(stringw(range_sep).c_str());
				return false;

			}
			else if (id == 4)
			{
				s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
				scaleFac_sep = (float)pos;
				((IGUIScrollBar*)event.GUIEvent.Caller)->setToolTipText(stringw(scaleFac_sep).c_str());
				return false;

			}
			else if (id == 5)
			{
				s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
				range_align = (float)pos;
				((IGUIScrollBar*)event.GUIEvent.Caller)->setToolTipText(stringw(range_align).c_str());
				return false;

			}
			else if (id == 6)
			{
				s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
				scaleFac_align = (float)pos/1000.0f;
				((IGUIScrollBar*)event.GUIEvent.Caller)->setToolTipText(stringw(scaleFac_align).c_str());
				return false;

			}
		}
		if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
		{
			if (id == 10)
			{
				//velocity += cohesion(boidList, 400.0f, .005f, boidListLength);
				//velocity += seperation(boidList, 200.0f, 200.0f, boidListLength);
				//velocity += alignment(boidList, 400.0f, .003f, boidListLength);
				range_align = 400.0f;
				range_cohes = 400.0f;
				range_sep = 200.0f;

				scaleFac_align = .003f;
				scaleFac_cohes = .005f;
				scaleFac_sep = 200.0f;

				sbRange_Align->setPos(s32(range_align));
				sbRange_cohes->setPos(s32(range_cohes));
				sbRange_Sep->setPos(s32(range_sep));

				sbScale_Align->setPos(s32(scaleFac_align*1000));
				sbScale_cohes->setPos(s32(scaleFac_cohes*1000));
				sbScale_Sep->setPos(s32(scaleFac_sep));

				return false;

			}

			
		}
		
	}
	
	else if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
	{
		if(event.KeyInput.Key == KEY_KEY_W){keyDown_W = true;return false;}
		if(event.KeyInput.Key == KEY_KEY_S){keyDown_S = true;return false;}
		if(event.KeyInput.Key == KEY_KEY_D){keyDown_D = true;return false;}
		if(event.KeyInput.Key == KEY_KEY_A){keyDown_A = true;return false;}
		if(event.KeyInput.Key == KEY_SPACE)
		{
			if (mouseMoveEnabled == false)
			{
				mouseMoveEnabled = true;
				mouseMoveJustChanged = true;
				return false;
			}
			else
			{
				mouseMoveEnabled = false;
				mouseMoveJustChanged = true;
				return false;
			}
		}
		if (event.KeyInput.Key == KEY_KEY_P)
		{
			if (paused == true){paused = false; return false;}
			else {paused = true; return false;}

		}

		

	}
	else if (event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
	{

		if(event.KeyInput.Key == KEY_KEY_W){keyDown_W = false;return false;}
		if(event.KeyInput.Key == KEY_KEY_S){keyDown_S = false;return false;}
		if(event.KeyInput.Key == KEY_KEY_D){keyDown_D = false;return false;}
		if(event.KeyInput.Key == KEY_KEY_A){keyDown_A = false;return false;}
		
	}
	else if (event.EventType == EET_MOUSE_INPUT_EVENT && !event.MouseInput.Event)
	{
		if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
		{
			std::cout<<"Left Mouse is pressed down" << std::endl;
			return false;
		}
		
	}

//<frodenius> well return false then the guiEnv will receive the event
return false;
}